package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.ChanPinXinXi;
import com.ysd.util.DBManager;

public class ChanPinXinXiDaoImpl {
	//根据产品分类ID查询对应的信息
	public List<ChanPinXinXi> getCPXXFenLeiId(int fenLeiID){
		List<ChanPinXinXi> cpxxList=new ArrayList<ChanPinXinXi>();
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		
		try {
			st=conn.createStatement();
			rs=st.executeQuery("SELECT * FROM chanpinxinxi WHERE fenLeiID="+fenLeiID);
			while(rs.next()) {
				int cpId=rs.getInt("cPId");
				String tp=rs.getString("tuPian");
				String cpName=rs.getString("cPName");
				ChanPinXinXi cpxx=new ChanPinXinXi(cpId,tp,cpName);
				cpxxList.add(cpxx);
			}
			return cpxxList;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	//根据产品id查询产品详情
	public List<ChanPinXinXi> getCPXXcPId(int pId){
		List<ChanPinXinXi> xx=new ArrayList<ChanPinXinXi>();
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		
		try {
			st=conn.createStatement();
			rs=st.executeQuery("SELECT * FROM chanpinxinxi WHERE cPId="+pId);
			while(rs.next()) {
				int cpId=rs.getInt("cPId");
				String tp=rs.getString("tuPian");
				String cpName=rs.getString("cPName");
				int shouJia=rs.getInt("cPShouJia");
				String pp=rs.getString("pinPai");
				String dz=rs.getString("shiFouDZ");
				String xqtpp=rs.getString("xQTpp");
				String xqtp=rs.getString("xQTp");
				String ys=rs.getString("chanPinYanSe");
				int fl=rs.getInt("fenLeiID");
				ChanPinXinXi cpxx=new ChanPinXinXi(cpId, tp, cpName, shouJia, pp, dz, xqtpp, xqtp, ys, fl);
				xx.add(cpxx);
			}
			return xx;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	//根据名称模糊查询产品信息
	
	public List<ChanPinXinXi> getCpXXCName(String cName){
		List<ChanPinXinXi> xxList=new ArrayList<ChanPinXinXi>();
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		
		try {
			st=conn.createStatement();
			rs=st.executeQuery("SELECT * FROM chanpinxinxi WHERE cPName LIKE '%"+cName+"%'");
			while(rs.next()) {
				int cpId=rs.getInt("cPId");
				String tp=rs.getString("tuPian");
				String cpName=rs.getString("cPName");
				ChanPinXinXi cp=new ChanPinXinXi(cpId, tp, cpName);
				xxList.add(cp);
			}
			return xxList;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally{
			DBManager.closeAll(conn, st, rs);
		}
	}
	
}
